<?php
namespace app\admin\controller;
use app\admin\controller\AdminController;
/**
 * 菜单管理
 */
class MenuController extends AdminController {
    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        return array(
            'menu' => array(
                    array(
                        'name' => '菜单列表',
                        'url' => url('index'),
                        'icon' => 'bars',
                    ),
                    array(
                        'name' => '添加菜单',
                        'url' => url('add'),
                        'icon' => 'plus',
                    ),
                ),
            );
    }

	/**
     * 列表
     */
    public function index(){
    	//管理目录导航
    	$this->adminCrumb[] = array('url'=>url('index'),'name'=>'菜单管理');
        $this->assign('list', target('forum/Menu')->loadList());
        $this->adminDisplay();
    }

    /**
     * 添加
     */
    public function add(){
    	if(!IS_POST){
	    	//管理目录导航
	    	$this->adminCrumb[] = array('url'=>url('index'),'name'=>'菜单管理');
	    	$this->adminCrumb[] = array('url'=>url('add'),'name'=>'菜单添加');
            $modelList = target('forum/Menu')->getModel();
	        $this->assign('modelList', $modelList);
            $this->assign('pageList', target('forum/Page')->loadList());
	        $this->assign('name','添加');
	        $this->adminDisplay('info');
        }else{
            if(target('forum/Menu')->saveData('add')){
                $this->success('菜单添加成功！',url('index'));
            }else{
                $msg = target('forum/Menu')->getError();
                if(empty($msg)){
                    $this->error('菜单添加失败');
                }else{
                    $this->error($msg);
                }
                
            }

        }
    }

    /**
     * 编辑
     */
    public function edit(){
    	if(!IS_POST){
    		$menuId = request('get.menu_id','','intval');
            if(empty($menuId)){
                $this->error('参数不能为空！');
            }
            $model = target('forum/Menu');
            $info = $model->getInfo($menuId);
            if(!$info){
                $this->error($model->getError());
            }
	    	//管理目录导航
	    	$this->adminCrumb[] = array('url'=>url('index'),'name'=>'菜单管理');
	    	$this->adminCrumb[] = array('url'=>url('add'),'name'=>'菜单添加');
	        $modelList = target('forum/Menu')->getModel();
            $this->assign('modelList', $modelList);
            $this->assign('pageList', target('forum/Page')->loadList());
	        $this->assign('name','编辑');
	        $this->assign('info',$info);
	        $this->adminDisplay('info');
        }else{
            if(target('forum/Menu')->saveData('edit')){
                $this->success('菜单编辑成功！',url('index'));
            }else{
                $msg = target('forum/Menu')->getError();
                if(empty($msg)){
                    $this->error('菜单编辑失败');
                }else{
                    $this->error($msg);
                }
                
            }

        }
    }

    /**
     * 删除
     */
    public function del(){
        $menuId = request('post.data');
        if(empty($menuId)){
            $this->error('参数不能为空！');
        }
        //删除菜单操作
        if(target('forum/Menu')->delData($menuId)){
            $this->success('菜单删除成功！');
        }else{
            $msg = target('forum/Menu')->getError();
            if(empty($msg)){
                $this->error('栏目删除失败！');
            }else{
                $this->error($msg);
            }
        }
    }
}

